package stx.com.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import stx.com.entity.TUser;
import stx.com.service.UserService;
import stx.com.util.Constans;

@Controller
public class UpdateController {

	@Resource
	private UserService userService;
	
	@RequestMapping("set.do")
	public String update(@RequestParam("photo") MultipartFile file,TUser user,HttpServletRequest request){
		if(file!=null&&!file.isEmpty()){
			File dir = new File(request.getSession().getServletContext().getRealPath("/")+Constans.STORE_PATH);
//			System.out.println(request.getSession().getServletContext().getRealPath("/")+Constans.STORE_PATH);
			String fileName=file.getOriginalFilename();
			try {
				file.transferTo(new File(dir,fileName));
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
			user.setRoute(Constans.STORE_PATH+fileName);
			if(userService.uUpdate(user)){
			return "main";
		}
		
	}
		return "false";
	}
}